package spring_cloud.demo.game;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class IndexController {

	@Autowired
	RestTemplate restTemplate;
	
	@GetMapping("/startGame")
	public String start(@RequestParam(defaultValue="Player1") String userName,@RequestParam(defaultValue="") String password) {
		return conectToServer(userName, password);
	}
	
	private  String conectToServer(String userName, String password) {
		Map<String, Object> uriVariables = new HashMap<>();
		uriVariables.put("name", userName);
		uriVariables.put("password", password);
		return restTemplate.getForEntity("http://SSO-SERVICE/login?userName={name}&password={password}", String.class,uriVariables).getBody();
	}
	
}
